<?php
class LoginModel extends Model{
	
	public $id;
	public $username;
	public $password;
	
	const salt = "b295d117135a9763da282e7dae73a5ca7d3e5b11";
	
	static function Check($username, $password)
	{
		$db = new DbContext();
		$connec = $db->getConnection();

		$query = "SELECT * FROM login WHERE username = ?";
		
		$q=$connec->prepare($query); 
		$q->execute(array($username)) or die(print_r($q->errorInfo(), true));
		$rows = $q->fetchAll(PDO::FETCH_ASSOC);
		return (count($rows) != 0 && hash_hmac('sha1', $password, self::salt) == $rows[0]['password']);
	}
	
	static function Insert($id, $username, $password)
	{
		$db = new DbContext();
		$connec = $db->getConnection();
		
		$query = "INSERT IGNORE INTO login(id, username, password) VALUES(:id, :username, :password)";
		$password = hash_hmac('sha1', $password , self::salt);
		$q=$connec->prepare($query); 
		$q->execute(array('id'=>$id, 'username'=>$username, 'password'=>$password));
	}	
	
	static function CreateHmac($password)
	{
		return hash_hmac('sha1', $password , self::salt);
	}
	
	static function GetFromUsername($username)
	{
		$db = new DbContext();
		$connec = $db->getConnection();

		$query = "SELECT * FROM login WHERE username = ?";

		$q=$connec->prepare($query);
		$q->execute(array($username)) or die(print_r($q->errorInfo(), true));
		
		$q->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'LoginModel'); 
		return $q->fetch();
	}	
}


?>
